package com.tony.dao.impl;

import com.tony.bean.Member;
import com.tony.dao.MemberDao;
import com.tony.dao.RedisGeneratorDao;
import org.springframework.dao.DataAccessException;
import org.springframework.data.redis.connection.RedisConnection;
import org.springframework.data.redis.core.RedisCallback;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.stereotype.Component;
import org.springframework.util.Assert;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Tony on 2017/1/1.
 */
@Component
public class MemberDaoImpl extends RedisGeneratorDao<String,Member> implements MemberDao {

    public  boolean add(final Member member){
        boolean result =  redisTemplate.execute(new RedisCallback<Boolean>() {
            public Boolean doInRedis(RedisConnection redisConnection) throws DataAccessException {
                RedisSerializer<String> serializer = getRedisSerializer();
                byte[] key = serializer.serialize(member.getId());
                byte[] name = serializer.serialize(member.getNickname());
                return redisConnection.setNX(key,name);
            }
        });

        return result;
    }

    public boolean add(final List<Member> memberList){
        Assert.notEmpty(memberList);
        boolean result = redisTemplate.execute(new RedisCallback<Boolean>() {
            public Boolean doInRedis(RedisConnection redisConnection) throws DataAccessException {
                RedisSerializer<String> serializer = getRedisSerializer();
                for (Member member : memberList) {
                    byte[] key  = serializer.serialize(member.getId());
                    byte[] name = serializer.serialize(member.getNickname());
                    redisConnection.setNX(key, name);
                }
                return true;
            }
        },false,true);
        return result;

    }

    /**
     * 删除对象 ,依赖key
     */
    public void delete(String key) {
        List<String> list = new ArrayList<String>();
        list.add(key);
        delete(list);
    }

    /**
     * 删除集合 ,依赖key集合
     */
    public void delete(List<String> keys) {
        redisTemplate.delete(keys);
    }

    /**
     * 修改对象
     */
    public boolean update(final Member member) {
        String key = member.getId();
        if (get(key) == null) {
            throw new NullPointerException("数据行不存在, key = " + key);
        }
        boolean result = redisTemplate.execute(new RedisCallback<Boolean>() {
            public Boolean doInRedis(RedisConnection connection)
                    throws DataAccessException {
                RedisSerializer<String> serializer = getRedisSerializer();
                byte[] key  = serializer.serialize(member.getId());
                byte[] name = serializer.serialize(member.getNickname());
                connection.set(key, name);
                return true;
            }
        });
        return result;
    }

    /**
     * 根据key获取对象
     */
    public Member get(final String keyId) {
        Member result = redisTemplate.execute(new RedisCallback<Member>() {
            public Member doInRedis(RedisConnection connection)
                    throws DataAccessException {
                RedisSerializer<String> serializer = getRedisSerializer();
                byte[] key = serializer.serialize(keyId);
                byte[] value = connection.get(key);
                if (value == null) {
                    return null;
                }
                String nickname = serializer.deserialize(value);
                return new Member(keyId, nickname);
            }
        });
        return result;
    }
}
